import { defineStore } from 'pinia';
import { computed } from 'vue';
import { useUserStore } from "@/stores/user.js";

export const useRoomStore = defineStore('room', {
    state: () => ({
        roomId: "",
        players: [],
        maxPlayers: 99,
        minPlayers: 2,
        hostId: "",
        isLoading: false
    }),

    actions: {
        updateRoomData(data) {
            if (data.roomId) {
                this.roomId = data.roomId;
            }

            this.players = Array.isArray(data.playerList)
                ? [...data.playerList]
                : [];

            this.hostId = data.hostId || '';
        },

        clearRoomData() {
            this.roomId = "";
            this.players = [];
            this.hostId = "";
            this.isLoading = false;
        }
    },

    getters: {
        isHost: (state) => {
            const userStore = useUserStore();
            return userStore.userId === state.hostId;
        },

        roomStatus: (state) => {
            if (state.players.length >= state.maxPlayers) return 'full';
            return 'waiting';
        }
    },

    persist: {
        enabled: true // Store中数据持久化生效
    }
});
